package com.sundog.sundroid.testclasses;

import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.filter.PacketTypeFilter;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;

import android.util.Log;

public class VotingSimulator {
	
	/** Create a packet filter to receive messages of type Message */
	PacketFilter filter = new PacketTypeFilter(Message.class);
	
	/** Create a packet listener to process messages */
	PacketListener myListener = new PacketListener() {
		
	public void processPacket(Packet packet) {
				
			Message msg = (Message) packet;
			
			Log.d("***", "Simulator received message: " + msg.getBody() + " from " + msg.getFrom());
			
	}};
	
	/** Connection configuration for XMPP messaging */
	private final ConnectionConfiguration connConfig =
		 new ConnectionConfiguration("talk.google.com", 80, "jabber.org"); 
	
	/** Establishes XMPP connection using the connection configuration */
	private final XMPPConnection connection1 = new XMPPConnection(connConfig);
	private final XMPPConnection connection2 = new XMPPConnection(connConfig);

	
	public void simulateVote() {
		
		try {
			connection1.connect();
			connection1.login("sundroiddemo@gmail.com", "sundog101");
			//connection1.addPacketListener(myListener, filter);

			connection2.connect();
			connection2.login("demosundog@gmail.com", "sundog101");
			//connection2.addPacketListener(myListener, filter);

		} catch(XMPPException e) {
			Log.d("ERROR", "XMPP Exception");
		}
		
		try {
			Thread.sleep(3000);
		} catch(Exception e) {
			Log.d("ERROR", "Thread exception");
		}
		
		Message message = new Message();
		
		message.setType(Message.Type.headline);
		message.setTo("demosundog@gmail.com");
		message.setBody("3");
	    connection1.sendPacket(message);
	    Log.d("***", "sundroiddemo sent message to demosundog");
	    
	    try {
			Thread.sleep(5000);
		} catch(Exception e) {
			Log.d("ERROR", "Thread exception");
		}
	    
	    message.setType(Message.Type.headline);
		message.setTo("sundroiddemo@gmail.com");
		message.setBody("5");
	    connection2.sendPacket(message);
	    Log.d("***", "demosundog sent message to sundroiddemo");
	}

}
